package com.abc.fast.server.user.service.impl;

import com.abc.fast.common.utils.SecurityUtil;
import com.abc.fast.server.user.dao.UserDao;
import com.abc.fast.common.domain.entity.User;
import com.abc.fast.server.user.domain.enums.RoleEnum;
import com.abc.fast.server.user.service.IUserRoleRelationService;
import com.abc.fast.server.user.service.IUserService;
import com.abc.fast.server.user.service.adapter.UserAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private IUserRoleRelationService userRoleRelationService;

    @Override
    public User selectByUsername(String username) {
        return userDao.selectByUsername(username);
    }

    @Override
    public boolean checkUsernameUnique(String username) {
        return userDao.checkUsernameUnique(username);
    }

    @Override
    @Transactional
    public void registerUser(String username, String password) {
        // 保存账号
        User user = UserAdapter.buildRegisterUser(username, SecurityUtil.bCryptPassword(password));
        userDao.save(user);
        // 保存用户身份
        userRoleRelationService.save(user.getId(), RoleEnum.USER.getRoleId());
    }
}
